package chapter02;

public class DeleteDuplicates83 {
    /**
     * 双指针
     * 申请一个头节点headNode 工作指针p指向头节点 p.next=null
     * head作为链表首元节点
     * 出现重复节点 head.next!=null&&head.val==head.next.val
     * 跳过重复节点 while(head.next!=null&&head.val==head.next.val){head=head.next}
     *     将head节点连接到p上
     *     p.next=head
     *     p=p.next
     *     head=head.next
     * 最后返回headNode.next
     */
    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    public ListNode deleteDuplicates(ListNode head) {
        ListNode headNode=new ListNode();
        ListNode p=headNode;
        while (head!=null){
            while (head.next!=null&&head.val==head.next.val){
                head=head.next;
            }
            p.next=head;
            p=p.next;
            head=head.next;
        }
        return headNode.next;
    }
}
